<%--
  Created by IntelliJ IDEA.
  User: Dhy
  Date: 2023/9/5
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加教师信息</title>
</head>
<body>
<%--通过form表单进行添加--%>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">课程名</label>
        <div class="layui-input-block">
            <input type="text" name="course_name" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学分</label>
        <div class="layui-input-block">
            <input type="text" name="course_credit" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">课程类型</label>
        <div class="layui-input-block">
            <input type="text" name="type" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学生</label>
        <div class="layui-col-md4">
            <div class="layui-input-inline">
                <select  name="student_id"></select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="teacher">添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="../layui/layui.js"></script>
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        $ = layui.$;    //全局变量

        // 提交事件
        form.on('submit(teacher)', function(data){
            var field = data.field; // 获取表单字段值

            //防止重复点击
            $(".layui-btn").addClass('layui-btn-disabled');
            $(".layui-btn").attr('disabled','disabled');

            // 此处可执行 Ajax 等操作
            $.ajax({
                url: '${pageContext.request.contextPath}/course?method=add',   // ajax请求地址 地址/开头是接口，直接写是页面
                data: field,  // 请求时传递的参数
                type: 'post',  // 请求的方式，get\post
                dataType: 'json', // 、json
                success: function(result){  // 请求成功时的回调， result请求返回的数据。
                    console.log(result);
                    if (result.code === 200){
                        layer.msg("添加成功");

                        //添加成功后跳转到list界面
                        $('#course_lookposlist').click();
                        $('.layui-body>div').load("${pageContext.request.contextPath}/Course/courseList.jsp");
                        // window.location.href="http://localhost:8080/"
                    }else {
                        layer.msg(result.msg);
                    }
                    //解放提交按钮
                    $(".layui-btn").removeClass('layui-btn-disabled');
                    $(".layui-btn").removeAttr('disabled');

                },
                error: function(result){   // 请求失败时的回调
                    console.log(result);
                    layer.msg(result.msg);
                }
            });

            return false; // 阻止默认 form 跳转
        });

        //获取学生信息
        $.ajax({
            url:'${pageContext.request.contextPath}/student?method=selectAll',
            type:'get',
            dataType:'json',
            success:function (result) {
                var html = '<option value="">请选择学生</option>';
                if (result.code === 0){
                    // alert(result.res);
                    //循环取数据
                    $.each(result.res,function (index,item) {
                        html += '<option value="'+ item.studentId+'">'+item.studentName+'</option>'
                        $('[name="student_id"]').html(html);
                        form.render();
                    })
                }
            }
        })

    });
</script>

</body>
</html>

